/**
* Copyright (c) 2025 Huawei Technologies Co., Ltd.
* This program is free software, you can redistribute it and/or modify it under the terms and conditions of
* CANN Open Software License Agreement Version 2.0 (the "License").
* Please refer to the License for details. You may not use this file except in compliance with the License.
* THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED,
* INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY, OR FITNESS FOR A PARTICULAR PURPOSE.
* See LICENSE in the root of the software repository for the full text of the License.
*/

#include <gtest/gtest.h>
#include <type_traits>
#include "kernel_operator.h"
using namespace std;
using namespace AscendC;


#define DTYPE half
#define DTYPE1 half
#define MODE 8

template <typename DstT, typename SrcT, int32_t mD>
class KernelVpack {
public:
    __aicore__ inline KernelVpack()
    {}
    __aicore__ inline void Init(GM_ADDR dst0Gm, GM_ADDR dst1Gm, GM_ADDR src0Gm, GM_ADDR src1Gm,
            uint32_t nums, uint32_t vecMask)
    {
        src1Global.SetGlobalBuffer(reinterpret_cast<__gm__ SrcT *>(src1Gm), nums);
        dst1Global.SetGlobalBuffer(reinterpret_cast<__gm__ DstT *>(dst1Gm), nums);

        pipe.InitBuffer(inQueueX2, 1, nums * sizeof(SrcT));
        pipe.InitBuffer(outQueue2, 1, nums * sizeof(DstT));
        dataSize = nums;
        mask = vecMask;
    }
    __aicore__ inline void Process()
    {
        CopyIn();
        Compute();
        CopyOut();
    }

private:
    __aicore__ inline void CopyIn()
    {
        LocalTensor<SrcT> src1Local = inQueueX2.AllocTensor<SrcT>();
        DataCopy(src1Local, src1Global, dataSize);
        inQueueX2.EnQue(src1Local);
    }
    __aicore__ inline void Compute()
    {
        LocalTensor<DstT> dst1Local = outQueue2.AllocTensor<DstT>();
        DstT zero = 0;
        LocalTensor<SrcT> src1Local = inQueueX2.DeQue<SrcT>();
        uint16_t maskBitSize = 256;
        uint16_t oneRepSize = maskBitSize/sizeof(DstT);
        uint16_t rep = dataSize/oneRepSize;
        __ubuf__ DstT* dst1Ptr = (__ubuf__ DstT*)dst1Local.GetPhyAddr();
        __ubuf__ SrcT* src1Ptr = (__ubuf__ SrcT*)src1Local.GetPhyAddr();
        __VEC_SCOPE__
        {
            MicroAPI::RegTensor<SrcT> vSrcReg1;
            MicroAPI::RegTensor<DstT> vDstReg1;
            uint32_t sreg = static_cast<uint32_t>(mask);
            MicroAPI::MaskReg maskReg;
            maskReg = MicroAPI::UpdateMask<DstT>(sreg);
            for (uint16_t i = 0; i < static_cast<uint16_t>(rep); i++) {
                DataCopy(vSrcReg1, src1Ptr + i * oneRepSize);
                if constexpr (mD == 0) {
                    Pack(vDstReg1, vSrcReg1);
                } else if constexpr (mD == 1) {
                    MicroAPI::UnPack(vDstReg1, vSrcReg1);
                }
                DataCopy(dst1Ptr + i * oneRepSize, vDstReg1, maskReg);
            }
        }
        outQueue2.EnQue<DstT>(dst1Local);
        inQueueX2.FreeTensor(src1Local);
    }
    __aicore__ inline void CopyOut()
    {
        LocalTensor<DstT> dst1Local = outQueue2.DeQue<DstT>();
        DataCopy(dst1Global, dst1Local, dataSize);
        outQueue2.FreeTensor(dst1Local);
    }

private:
    GlobalTensor<SrcT> src1Global;
    GlobalTensor<DstT> dst1Global;

    TPipe pipe;
    TQue<TPosition::VECIN, 1> inQueueX2;
    TQue<TPosition::VECOUT, 1> outQueue2;
    uint32_t dataSize = 0;
    uint32_t mask;
};

struct MicroVpackParams {
    void (*CallFunc)();
};

template<typename DstT, typename SrcT, int32_t mode>
void RunCase() {
    int byteSize = sizeof(DstT);
    int shapeSize = 1024;
    int mask = 256;
    int dataSize = 1024;
    uint8_t dst0Gm[shapeSize * byteSize] = {0};
    uint8_t dst1Gm[shapeSize * byteSize] = {0};
    uint8_t src0Gm[shapeSize * byteSize] = {0};
    uint8_t src1Gm[shapeSize * byteSize] = {0};

    KernelVpack<DstT, SrcT, mode> op;
    op.Init(dst0Gm, dst1Gm, src0Gm, src1Gm, dataSize, mask);
    op.Process();
}

class MicroVpackTestsuite : public testing::Test, public testing::WithParamInterface<MicroVpackParams> {
protected:
    void SetUp() {}
    void TearDown() {}
};

INSTANTIATE_TEST_CASE_P(MicroVpackTestCase, MicroVpackTestsuite,
    ::testing::Values(
                    MicroVpackParams { RunCase<uint8_t, uint16_t, 0> },
                    MicroVpackParams { RunCase<uint8_t, int16_t, 0> },
                    MicroVpackParams { RunCase<uint16_t, uint32_t, 0> },
                    MicroVpackParams { RunCase<uint16_t, int32_t, 0> },
                    MicroVpackParams { RunCase<uint32_t, uint16_t, 1> },
                    MicroVpackParams { RunCase<int32_t, int16_t, 1> },
                    MicroVpackParams { RunCase<uint16_t, uint8_t, 1> },
                    MicroVpackParams { RunCase<int16_t, int8_t, 1> }
                        ));

TEST_P(MicroVpackTestsuite, MicroVpackTestCase)
{
    auto param = GetParam();
    param.CallFunc();
}
